package cn.ytxy.ivos.vehicle.service.Impl;


import cn.ytxy.ivos.base.response.JsonResult;
import cn.ytxy.ivos.vehicle.mapper.VehicleMapper;
import cn.ytxy.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.ytxy.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.ytxy.ivos.vehicle.pojo.entity.Vehicle;
import cn.ytxy.ivos.vehicle.pojo.vo.VehicleVO;
import cn.ytxy.ivos.vehicle.service.VehicleService;
import com.fasterxml.jackson.databind.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName VehicleServiceImpl
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/4/28 16:19
 * @Version 1.0
 */

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;
    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehiclequery) {
        log.debug("查询车辆信息，VehicleQuery={}", vehiclequery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehiclequery);
        return list;
    }

    @Override
    public void saveVehicle(VehicleSaveParam VehicleSaveParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(VehicleSaveParam, vehicle);
        if (vehicle.getId() == null) {//新增
            log.debug("service层，保存车辆信息，VehicleSaveParam={}", VehicleSaveParam);
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicleMapper.insert(vehicle);
        }else {//有id就是更新
            log.debug("service层，更新车辆信息，VehicleSaveParam={}", VehicleSaveParam);
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("service层删除车辆信息，id={}", id);
        vehicleMapper.delete(id);
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        log.debug("service层解绑车辆信息，vehicleId={}", vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置车辆id也就是改那辆车的数据
        vehicle.setGeofenceBindStatus("0");//围栏绑定状态，置为0
        vehicle.setGeofenceId(null);//电子围栏id置为null
        vehicle.setUpdateTime(new Date());//更新时间
//        之前的更新sql要求围栏编号不能为null才会更新围栏编号
//        但此处由于业务=>将车辆与围栏解除绑定关系
//        需要将围栏编号置为null，所以需要重写一个mapper层的方法
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geoId, Long vehicleId) {
        log.debug("service层绑定车辆信息，geoId={},vehicleId={}", geoId, vehicleId);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geoId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
